package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.common.core.utils.MapstructUtils;
import org.example.common.core.utils.page.Pager;
import org.example.common.datasource.utils.SearchParamsHelper;
import org.example.system.domain.SysLoginLog;
import org.example.system.domain.bo.SysLoginLogBo;
import org.example.system.domain.vo.SysLoginLogVo;
import org.example.system.mapper.SysLoginLogMapper;
import org.example.system.service.ISysLoginLogService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 *
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class SysLoginLogServiceImpl implements ISysLoginLogService {

    private final SysLoginLogMapper baseMapper;


    @Override
    public Pager<SysLoginLogVo> selectPageLogininforList(Pager pager) {
        SearchParamsHelper.loadSearchParam(pager, Wrappers.query(),(p,w)->baseMapper.selectVoPage(p,w));
        return pager;
    }


    /**
     * 查询系统登录日志集合
     *
     * @param pager 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginLogVo> selectLogininforList(Pager pager) {
        QueryWrapper queryWrapper=Wrappers.query();
        SearchParamsHelper.loadSearchParam(pager,queryWrapper);
        return baseMapper.selectVoList(queryWrapper);
    }


    /**
     * 新增系统登录日志
     *
     * @param bo 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLoginLogBo bo) {
        SysLoginLog logininfor = MapstructUtils.convert(bo, SysLoginLog.class);
        logininfor.setLoginTime(new Date());
        baseMapper.insert(logininfor);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(infoIds));
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        baseMapper.delete(new LambdaQueryWrapper<>());
    }
}
